var flag0=false;
var flag1=false;
var flag2=false;
var flag3=false;
var flag4=false;
function cc() {
    code="";       /* 重置验证码*/
    var codel=4;
    var checkcode=document.getElementById("code");    /* 传值给验证码*/
    var random=new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    for(var i=0; i<codel;i++) {
        var index = Math.floor(Math.random()*36);
        code += random[index];
    }
    checkcode.value=code;
}
cc();
var RUN = document.getElementById("RUname");
var RUP = document.getElementById("RUpassword");
var QUP = document.getElementById("QRUpassword");
var RUM = document.getElementById("RUmail");
var RYZ = document.getElementById("YZ");
var COD = document.getElementById("code");
var SZC = document.getElementById("Registersub");
var SDL = document.getElementById("Loginsub");
var LGB = document.getElementById("Lx");
var RGB = document.getElementById("Rx");
RUN.onblur=function(){
    var re =/.{1,10}/;
    if(re.test(this.value)){
        this.style.borderColor="green";
        flag0=true;
    }else{
        this.style.borderColor="red";
        flag0=false;
    }
}
RUP.onblur=function () {
    var g = /^\w{6,16}$/;
    if (g.test(this.value)){
        this.style.borderColor="green";
        flag1=true;
    }else{
        this.style.borderColor="red";
        flag1=false;
    }
}
QUP.onblur=function () {
    if (this.value==RUP.value){
        this.style.borderColor="green";
        flag2=true;
    }else {
        this.style.borderColor="red";
        flag2=false;
    }
}
RUM.onblur=function () {
    var re =/^([a-zA-Z0-9]+[_|_|\-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/;
    if(re.test(this.value)){
        this.style.borderColor="green";
        flag3=true;
    }else{
        this.style.borderColor="red";
        flag3=false;
    }
}
RYZ.onblur=function () {
    if (this.value==COD.value){
        this.style.borderColor="green";
        flag4=true;
    }else {
        this.style.borderColor="red";
        flag4=false;
    }
}
LGB.onclick=function () {
    document.getElementById("Login").style.display="none";
    document.getElementById("MC").style.display="none";
}
RGB.onclick=function () {
    document.getElementById("Register").style.display="none";
    document.getElementById("MC").style.display="none";
}
SZC.onclick=function () {
    if (flag0&&flag1&&flag2&&flag3&&flag4){
        // alert("正在注册，请稍等...");
        var name = document.getElementById("RUname").value;
        var pwd = document.getElementById("RUpassword").value;
        var mail = document.getElementById("RUmail").value;
        var request = ajaxFunction();
        // 2 发送请求 参数1:选择发送方式  参数2:发送地址(这里发送给一个Servlet)  参数3:是否异步传输(一般为true)
        request.open("POST", "RS", true);
        // 3 获取请求响应数据 注册监听  状态如果发生变化 会执行等号右边的函数
        request.onreadystatechange = function() {
            //前半段表示 已经能够正常处理。  再判断状态码是否是200
            if (request.readyState == 4 && request.status == 200) {
                //弹出响应的信息
                var test = eval("("+request.responseText+")");
                alert(test[0].SQLMSG);
                if (test[0].STATE){
                    GBLogin();
                }
            }
        }
        request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        request.send("Uname="+name+"&Upassword="+pwd+"&Umail="+mail);
    }
    else{
        alert("请完整填写信息！");
    }
}
SDL.onclick=function () {
    var name = document.getElementById("Uname").value;
    var pass = document.getElementById("Upassword").value;
    var request1 = ajaxFunction();
    // 2 发送请求 参数1:选择发送方式  参数2:发送地址(这里发送给一个Servlet)  参数3:是否异步传输(一般为true)
    request1.open("POST", "./../LoginServlet", false);
    // 3 获取请求响应数据 注册监听  状态如果发生变化 会执行等号右边的函数
    request1.onreadystatechange = function() {
        //前半段表示 已经能够正常处理。  再判断状态码是否是200
        if (request1.readyState == 4 && request1.status == 200) {
            
        }
    }
    request1.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    request1.send("Uname="+name+"&Upassword="+pass);
}
function CBLogin() {
    document.getElementById("Login").style.display="block";
    document.getElementById("MC").style.display="block";
}
function CBRegister() {
    document.getElementById("Register").style.display="block";
    document.getElementById("MC").style.display="block";
}
function GBRegister() {
    window.location='Register.jsp';
}
function GBLogin() {
    document.getElementById("Register").style.display="none";
    document.getElementById("Login").style.display="block";
}
function LGExit() {
    window.localStorage.removeItem("Uid");
    window.localStorage.removeItem("Uname");
    alert("退出成功");
    window.location.reload();
}